package com.uu.safeguard.controller;

import com.uu.safeguard.result.Result;
import com.uu.safeguard.result.ResultFactory;
import com.uu.safeguard.utils.CommonUtil;
import com.uu.safeguard.utils.DbUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stationOfflineLog")
public class StationOfflineLogController {
    /**
     * @Description: 设备离线日志
     * @Param: [token, stationSn, limit, page]
     **/
    @PostMapping("list")
    public Result list(@RequestHeader("token") String token, String stationSn, Integer limit, Integer page){
        if(stationSn == null || StringUtils.isBlank(stationSn)){
            return ResultFactory.buildFailResult("设备编号不能为空");
        }
        long nowTime = System.currentTimeMillis();
        if(limit == null ){
            limit = 100;
        }
        if(page == null ){
            page = 1;
        }
        List list = new ArrayList();
        int count = 0;

        DbUtil db = new DbUtil();

        String list_sql = "select station_offline_log.* from station_offline_log left join station on station.sn = station_offline_log.station_sn where station_offline_log.create_time >= station.install_time and station_offline_log.station_sn = ? order by id desc";
        List parameter = new ArrayList<Object>(){{
            add(stationSn);
        }};
        list = db.executeQueryBySql(list_sql,parameter);

        String count_sql = "select count(station_offline_log.id) as count from station_offline_log left join station on station.sn = station_offline_log.station_sn where station_offline_log.create_time >= station.install_time and station_offline_log.station_sn = ?";
        List<Map> count_list = db.executeQueryBySql(count_sql,parameter);
        if(count_list.size() == 0){
            return ResultFactory.buildFailResult("操作失败");
        }
        count = Integer.parseInt(count_list.get(0).get("count").toString());

        for (int i = 0; i < list.size(); i++) {
            Map o = (Map) list.get(i);
            long offlineTime =  Long.parseLong(o.get("offline_time").toString());
            long onlineTime =  Long.parseLong(o.get("online_time").toString());
            o.put("_offline_time",CommonUtil.stampToDate(offlineTime));
            o.put("_online_time",CommonUtil.stampToDate(onlineTime));
            Map offlineDuration = CommonUtil.stampToDuration(onlineTime - offlineTime);
            o.put("offline_duration",offlineDuration);
            if(onlineTime > 0){
                o.put("offline_duration_text",CommonUtil.stampToDurationStr(onlineTime - offlineTime));
            }else{
                o.put("offline_duration_text",CommonUtil.stampToDurationStr(nowTime - offlineTime));
                o.put("_online_time","未上线");
            }

            list.set(i,o);
        }

        Map data = new HashMap();
        data.put("list", list);
        data.put("count",count);
        return ResultFactory.buildSuccessResult(data);

    }
}
